package com.mango.community.controller;

import com.mango.community.entity.Comment;
import com.mango.community.entity.DiscussPost;
import com.mango.community.entity.Page;
import com.mango.community.entity.User;
import com.mango.community.service.CommentService;
import com.mango.community.service.DiscussPostService;
import com.mango.community.service.LikeService;
import com.mango.community.service.UserService;
import com.mango.community.util.CommunityConstant;
import com.mango.community.util.CommunityUtil;
import com.mango.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2020/7/26 15:53
 * 帖子相关
 */
@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private LikeService likeService;
    @PostMapping("/add")
    @ResponseBody
    public String addDiscuss(String title,String content){
        //获取user的信息
        User user = hostHolder.getUser();
        //判断user
        if(user == null){
            return CommunityUtil.getJSONString(403,"你还没有登录!");
        }
        //初始化一个对象
        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle(title);
        discussPost.setContent(content);
        discussPost.setCreateTime(new Date());
        //执行新增
        discussPostService.addDiscussPost(discussPost);
        //向前台返回数据
        return CommunityUtil.getJSONString(0,"发布成功");
    }

    @GetMapping("/detail/{id}")
    public String getDiscussPost(@PathVariable("id") int id, Model model , Page page){
        //查询帖子
        DiscussPost discussPost = discussPostService.findDiscussPostById(id);
        model.addAttribute("post",discussPost);
        //查询帖子作者的相关信息
        User user = userService.findUserById(discussPost.getUserId());
        model.addAttribute("user",user);
        //查询帖子点赞的数量
        long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST,id);
        model.addAttribute("likeCount",likeCount);
        //查询帖子的点赞状态 当用户登录时才去查询否则直接返回0
        int likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_POST,id);
        model.addAttribute("likeStatus",likeStatus);
        //查评论的分页信息
        //设置分页的页数
        page.setLimit(5);
        //设置分页的查询路径
        page.setPath("/discuss/detail/" + id);
        //设置分页的总数
        page.setRows(discussPost.getCommentCount());
        //分页查询评论
        //评论：给帖子的评论
        //回复：给帖子的评论
        List<Comment> commentList = commentService.findCommentByEntity(ENTITY_TYPE_POST, discussPost.getId(), page.getOffset(), page.getLimit());
        //评论的VO列表
        List<Map<String,Object>> commentVOList = new ArrayList<>();
        if (commentList != null){
            //将 获取的数据commentList 遍历
            for (Comment comment : commentList) {
                //一个评论的VO
                Map<String,Object> commentVO = new HashMap<>();
                //将评论的数据放入到map中
                commentVO.put("comment",comment);
                //将用户的信息放入到map中
                commentVO.put("user",userService.findUserById(comment.getUserId()));
                //查询评论点赞的数量
                likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,comment.getId());
                commentVO.put("likeCount",likeCount);
                //查询评论的点赞状态 当用户登录时才去查询否则直接返回0
                likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,comment.getId());
                commentVO.put("likeStatus",likeStatus);

                //查询回复列表
                List<Comment> replyList = commentService.findCommentByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                //回复的VO列表
                List<Map<String,Object>> replyVOList = new ArrayList<>();
                if(replyList !=null ){
                    for (Comment reply : replyList) {
                        Map<String,Object> replyVO = new HashMap<>();
                        //将回复数据放入到map中
                        replyVO.put("reply",reply);
                        //将用户信息放入到map中
                        replyVO.put("user",userService.findUserById(reply.getUserId()));
                        //回复的目标
                        User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
                        replyVO.put("target",target);
                        //查询回复点赞的数量
                        likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,reply.getId());
                        replyVO.put("likeCount",likeCount);
                        //查询回复的点赞状态 当用户登录时才去查询否则直接返回0
                        likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,reply.getId());
                        replyVO.put("likeStatus",likeStatus);
                        replyVOList.add(replyVO);
                    }
                }
                //将回复的列表集合放到评论的map中
                commentVO.put("replys",replyVOList);
                //回复的数量
                int  replyCount= commentService.findCommentCount(ENTITY_TYPE_COMMENT,comment.getId());
                //将回复的数量放入到评论的map中
                commentVO.put("replyCount",replyCount);
                commentVOList.add(commentVO);
            }
        }
        model.addAttribute("comments",commentVOList);
        return "/site/discuss-detail";
    }


}
